using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TesteIATcc;

namespace TesteIATcc.Comuns
{
    /// <summary>
    /// Classe responsavel por imprimir um sprite em tela
    /// </summary>
    public class Sprite : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Point posicaoSprite = new Point(0, 0);
        private Vector2 tamanhoSprite = new Vector2(0f, 0f);
        private Texture2D texturaSprite;
        private float rotacaoSprite = 0f;
        private SpriteEffects efeitoSprite = SpriteEffects.None;
        private int profundidadeDesenho = 0;
        private string caminhoImagem;

        private float timerDoDraw = 0f;
        private float intervaloDoDraw = 0.245f;
        
        private SpriteBatch sb;

        public Vector2 TamanhoSprite
        {
            get { return tamanhoSprite; }
        }
        public SpriteEffects EfeitoSprite
        {
            get { return efeitoSprite; }
            set { efeitoSprite = value; }
        }       
        public float RotacaoSprite
        {
            get { return rotacaoSprite; }
            set { rotacaoSprite = value; }
        }
        public Point PosicaoSprite
        {
            get { return posicaoSprite; }
            set { posicaoSprite = value; }
        }

        public Sprite(TesteIA game, string caminhoDaImagem, Vector2 tamanho, int profundidade)
            : base(game)
        {
            this.tamanhoSprite = tamanho;
            this.caminhoImagem = caminhoDaImagem;
            this.profundidadeDesenho = profundidade;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            sb = new SpriteBatch(base.Game.GraphicsDevice);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            this.texturaSprite = base.Game.Content.Load<Texture2D>(this.caminhoImagem);
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            timerDoDraw += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timerDoDraw > intervaloDoDraw)
            {               
                timerDoDraw = 0f;
                sb.Begin();
                sb.Draw(this.texturaSprite,
                    new Rectangle(this.posicaoSprite.X,
                        this.posicaoSprite.Y,
                        (int)this.tamanhoSprite.X,
                        (int)this.tamanhoSprite.Y),
                    null,
                    Color.White,
                    this.rotacaoSprite,
                    new Vector2(0f, 0f),
                    this.efeitoSprite,
                    this.profundidadeDesenho);
                sb.End();

                base.Draw(gameTime);
            }
        }
    }
}